package com.sanqing.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JTextField;

import com.sanqing.model.BookInfo;
import com.sanqing.model.BookType;
import com.sanqing.model.Borrow;
import com.sanqing.model.Operator;
import com.sanqing.model.OrderInfo;
import com.sanqing.model.RecommenderInfo;
import com.sanqing.model.RecordInfo;
import com.sanqing.model.Reserve;

public class Dao {
	protected static String dbClassName = "com.mysql.jdbc.Driver";
	protected static String dbUrl = "jdbc:mysql://localhost:3306/db_library";
	protected static String dbUsr = "root"; // 数据库用户名
	protected static String dbPwd = "123"; // 数据库密码
	private static Connection conn = null; // 数据库连接对象
	// 构造方法

	private Dao() {
		try {
			if (conn == null) { // 如果连接对象为空
				Class.forName(dbClassName); // 加载驱动类
				conn = DriverManager.getConnection(dbUrl, dbUsr, dbPwd); // 连接，获取连接对象
			}
		} catch (Exception ee) {
			ee.printStackTrace();
		}
	}

	// 转义 SQL 字符串中的单引号
	private static String escapeSql(String input) {
		if (input == null)
			return null;
		return input.replace("'", "''");
	}

	// 查询方法
	public static ResultSet executeQuery(String sql) {
		try {
			if (conn == null)
				new Dao(); // 连接对象为空，重新调用构造方法
			return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE).executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
		}
	}

	// 更新方法
	public static int executeUpdate(String sql) {
		try {
			if (conn == null)
				new Dao(); // 连接对象为空，重新调用构造方法
			return conn.createStatement().executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		} finally {
		}
	}

	// 关闭方式
	public static void close() {
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			conn = null;
		}
	}

	public static Operator check(String R_ISBN, String password) {
		Operator operator = new Operator();// 操作员信息对象
		String sql = "select *  from tb_operator where R_ISBN='" + R_ISBN + "' and password='" + password + "';";// 查询字符串
		System.out.println("执行更新SQL: " + sql); // 调试用
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) {// 如果查询到了记录
				operator.setSex(rs.getString("sex"));// 设置操作员用户名
				operator.setName(rs.getString("name"));// 设置操作员用户名
				operator.setIndentityCard(rs.getString("identityCard"));// 设置操作员用户名
				operator.setDate(rs.getDate("date"));// 设置操作员用户名
				operator.setMaxNum(rs.getString("maxNum"));// 设置操作员用户名
				operator.setTel(rs.getString("tel"));// 设置操作员用户名
				operator.setZj(rs.getInt("zj"));// 设置操作员用户名
				operator.setZy(rs.getString("zy"));// 设置操作员用户名
				operator.setISBN(rs.getString("R_ISBN"));// 设置操作员用户名
				operator.setGrade(rs.getInt("admin"));// 设置操作员等级
				operator.setPassword(rs.getString("password"));// 设置管理员密码
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return operator;// 返回操作员信息对象
	}

	public static List selectBookinfo(String sql) {// 根据图书编号查询图书
		List list = new ArrayList();// 保存所有查询到书籍信息
		System.out.println("执行更新SQL: " + sql); // 调试用
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) { // 遍历结果集
				BookInfo bookinfo = new BookInfo(); // 书籍信息对象
				bookinfo.setISBN(rs.getString("ISBN")); // 设置书籍编号
				bookinfo.setTypeid(rs.getInt("typeid")); // 设置类别编号
				bookinfo.setBookname(rs.getString("bookname"));// 设置书籍名称
				bookinfo.setWriter(rs.getString("writer")); // 设置作者
				bookinfo.setTranslator(rs.getString("translator")); // 设置译者
				bookinfo.setPublisher(rs.getString("publisher")); // 设置出版社
				bookinfo.setLanguage(rs.getString("language")); // 设置出版社
				bookinfo.setDate(rs.getDate("date")); // 设置出版日期
				bookinfo.setPrice(rs.getDouble("price")); // 设置价格
				bookinfo.setNumber(rs.getInt("number"));
				list.add(bookinfo); // 添加书籍信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return list; // 返回查询结果集
	}

	public static List selectBorrow(String sql)// 查阅Borrow表
	{
		List list = new ArrayList();// 保存所有查询到书籍信息
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) { // 遍历结果集
				Borrow borrow = new Borrow(); // 书籍信息对象
				borrow.setId(rs.getInt("id")); // 设置借书id
				borrow.setReaderISBN(rs.getString("readerISBN")); // 设置设置读者ISBN
				borrow.setBookISBN(rs.getString("bookISBN"));// 设置书籍的ISBN
				borrow.setISback(rs.getInt("isback")); // 设置是否归还
				borrow.setBorrowDate(rs.getString("borrowDate")); // 设置借书日期
				borrow.setBackDate(rs.getString("backDate")); // 设置还书日期
				// System.out.println(borrow.getBackDate());
				list.add(borrow); // 添加书籍信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return list;
	}

	public static List selectBookInfo() {// 根据图书编号查询图书
		String sql = "select *  from tb_bookInfo";
		return selectBookinfo(sql);
	}

	public static List selectBookInfo(String ISBN) {// 根据图书编号查询图书
		String sql = "select *  from tb_bookInfo where ISBN='" + ISBN + "'";
		return selectBookinfo(sql);
	}
	
	public static List selectBookAuthor(String BookAuthor) {// 根据图书编号查询图书
		String sql = "select *  from tb_bookInfo where writer like '%'" + BookAuthor + "%'";
		return selectBookinfo(sql);
	}
	public static List selectBookTranslator(String BookTranslator) {// 根据图书编号查询图书
		String sql = "select *  from tb_bookInfo where translator like'%'" + BookTranslator + "%'";
		return selectBookinfo(sql);
	}
	public static List selectBookLanguage(String Language) {// 根据图书编号查询图书
		String sql = "select *  from tb_bookInfo where language like'% '" + Language + "%'";
		return selectBookinfo(sql);
	}
	public static List selectBookname(String bookname) {// 根据图书姓名查询图书

		String sql = "select *  from tb_bookInfo where bookname like'%" + bookname + "%'";
		return selectBookinfo(sql);
	}

	public static List selectBookCategory() {// 查询所有书籍类型
		String sql = "select *  from tb_bookType  ";
		return selectBookType(sql);
	}

	public static List selectBookType(String sql) {// 根据图书编号查询图书
		List list = new ArrayList();// 保存所有查询到书籍信息
		System.out.println("执行更新SQL: " + sql); // 调试用
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) { // 遍历结果集
				BookType booktype = new BookType(); // 书籍信息对象
				booktype.setId(rs.getInt("id")); // 设置书籍编号
				booktype.setTypeName(rs.getString("typeName")); // 设置类别编号
				list.add(booktype); // 添加书籍信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return list; // 返回查询结果集
	}

	public static List selectBookPrice(int idx, double price)// 查询价格
	{
		String sql = "select *  from tb_bookInfo where price=" + price;
		if (idx == 1)
			sql = "select *  from tb_bookInfo where price>=" + price;
		else if (idx == 2)
			sql = "select *  from tb_bookInfo where price>" + price;
		else if (idx == 3)
			sql = "select *  from tb_bookInfo where price<=" + price;
		else if (idx == 4)
			sql = "select *  from tb_bookInfo where price<" + price;
		return selectBookinfo(sql);
	}

	public static List selectBookTimeout(String username, String nowDate)// 查询是否所有存在超时的书未归还
	{
		String sql = "select * from tb_borrow where isback=0 and readerISBN ='" + username + "' and backDate<'"
				+ nowDate + "'";
		return selectBorrow(sql);
	}
	

	public static List selectBookCategory(String BookType) {// 查询所有书籍类型
		String sql = "select *  from tb_bookInfo where typeId = (select id from tb_booktype where typeName like'%" + BookType
				+ "%')";
		return selectBookinfo(sql);
	}

	public static List selectBookPublisher(String publisher) {// 查询所有书籍类别
		String sql = "select *  from tb_bookInfo where publisher = '" + publisher + "'";
		return selectBookinfo(sql);
	}

	public static List selectOrderInfo() { // 查询所有书籍类型
		List list = new ArrayList();// 保存所有查询到书籍类型
		String sql = "select *  from tb_order  ";
		System.out.println("执行更新SQL: " + sql); // 调试用
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) { // 遍历结果集
				OrderInfo orderInfo = new OrderInfo(); // 书籍信息对象
				orderInfo.setISBN(rs.getString("ISBN")); // 设置书籍编号
				orderInfo.setTypeid(rs.getInt("typeid")); // 设置类别编号
				orderInfo.setBookname(rs.getString("bookname"));// 设置书籍名称
				orderInfo.setWriter(rs.getString("writer")); // 设置作者
				orderInfo.setTranslator(rs.getString("translator")); // 设置译者
				orderInfo.setPublisher(rs.getString("publisher")); // 设置出版社
				orderInfo.setLanguage(rs.getString("language")); // 设置出版社
				orderInfo.setDate(rs.getDate("date")); // 设置出版日期
				orderInfo.setReason(rs.getString("reason"));
				orderInfo.setRecommender(rs.getString("recommender"));

				list.add(orderInfo); // 添加书籍信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return list; // 返回查询结果集
	}

	public static List selectOrderInfo(String ISBN) { // 查询所有书籍类型
		List list = new ArrayList();// 保存所有查询到书籍类型
		String sql = "select *  from tb_order where ISBN= '" + ISBN + "'";
		System.out.println("执行更新SQL: " + sql); // 调试用
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) { // 遍历结果集
				OrderInfo orderInfo = new OrderInfo(); // 书籍信息对象
				orderInfo.setISBN(rs.getString("ISBN")); // 设置书籍编号
				orderInfo.setTypeid(rs.getInt("typeid")); // 设置类别编号
				orderInfo.setBookname(rs.getString("bookname"));// 设置书籍名称
				orderInfo.setWriter(rs.getString("writer")); // 设置作者
				orderInfo.setTranslator(rs.getString("translator")); // 设置译者
				orderInfo.setPublisher(rs.getString("publisher")); // 设置出版社
				orderInfo.setDate(rs.getDate("date")); // 设置出版日期
				orderInfo.setReason(rs.getString("reason"));
				orderInfo.setRecommender(rs.getString("recommender"));
				list.add(orderInfo); // 添加书籍信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return list; // 返回查询结果集
	}

	public static List<RecommenderInfo> selectRecommenderInfo(String username) { // 查询所有书籍类型
		List<RecommenderInfo> list = new ArrayList();// 保存所有查询到书籍类型
		String sql = "select *  from tb_recommender where username= '" + username + "'";
		System.out.println("执行更新SQL: " + sql); // 调试用
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) { // 遍历结果集
				RecommenderInfo recommenderinfo = new RecommenderInfo(); // 书籍信息对象
				recommenderinfo.setUsername(rs.getString("username"));
				recommenderinfo.setISBN(rs.getString("ISBN"));
				list.add(recommenderinfo); // 添加书籍信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return list; // 返回查询结果集
	}

	public static List selectRecordInfo() { // 查询所有书籍类型
		List list = new ArrayList();// 保存所有查询到书籍类型
		String sql = "select *  from tb_record";
		System.out.println("执行更新SQL: " + sql); // 调试用
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) { // 遍历结果集
				RecordInfo recordinfo = new RecordInfo(); // 书籍信息对象
				recordinfo.setISBN(rs.getString("ISBN")); // 设置书籍编号
				recordinfo.setTypeid(rs.getInt("typeid")); // 设置类别编号
				recordinfo.setBookname(rs.getString("bookname"));// 设置书籍名称
				recordinfo.setWriter(rs.getString("writer")); // 设置作者
				recordinfo.setTranslator(rs.getString("translator")); // 设置译者
				recordinfo.setPublisher(rs.getString("publisher")); // 设置出版社
				recordinfo.setDate(rs.getDate("date")); // 设置出版日期
				recordinfo.setLanguage(rs.getString("language"));
				recordinfo.setReason(rs.getString("reason"));
				recordinfo.setNumber(rs.getInt("number"));
				recordinfo.setOperator(rs.getString("operator"));
				recordinfo.setPrices(rs.getDouble("prices"));
				list.add(recordinfo); // 添加书籍信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return list; // 返回查询结果集
	}

	public static int Insertbook(String ISBN, Integer typeId, String bookname, String writer, String translator,
			String publisher, String languange, Date date, Double price, Integer number) {
		int i = 0;
		try {
			// 修正 SQL 语句，添加引号
			String sql = "INSERT INTO tb_bookinfo(ISBN, typeId, bookname, writer, translator, "
					+ "publisher,language, number,date, price) VALUES (" + "'" + escapeSql(ISBN) + "', " + typeId + ", "
					+ "'" + escapeSql(bookname) + "', " + "'" + escapeSql(writer) + "', " + "'" + escapeSql(translator)
					+ "', " + "'" + escapeSql(languange) + "', " + "'" + escapeSql(publisher) + "', " + +number + ", "
					+ "'" + date + "', " + price + ")";

			System.out.println("执行插入SQL: " + sql); // 调试用
			i = Dao.executeUpdate(sql);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		Dao.close();
		return i;
	}

	public static int Insertorder(String ISBN, Integer typeId, String bookname, String writer, String translator,
			String publisher, String languange, Date date, String recommender, String reason) {
		int i = 0;
		try {
			// 修正 SQL 语句，添加引号
			String sql = "INSERT INTO tb_order(ISBN, typeId, bookname, writer, translator, "
					+ "publisher,language,recommender,date, reason) VALUES (" + "'" + escapeSql(ISBN) + "', " + typeId
					+ ", " + "'" + escapeSql(bookname) + "', " + "'" + escapeSql(writer) + "', " + "'"
					+ escapeSql(translator) + "', " + "'" + escapeSql(publisher) + "', " + "'" + escapeSql(languange)
					+ "', " + "'" + escapeSql(recommender) + "', " + "'" + date + "', " + "'" + escapeSql(reason)
					+ "')";

			System.out.println("执行插入SQL: " + sql); // 调试用
			i = Dao.executeUpdate(sql);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		Dao.close();
		return i;
	}

	public static int Insertrecord(String ISBN, Integer typeId, String bookname, String writer, String translator,
			String publisher, String languange, Date date, String reason, Double prices, Integer nums,
			String operator) {
		int i = 0;
		try {
			// 修正 SQL 语句，添加引号
			String sql = "INSERT INTO tb_record(ISBN, typeId, bookname, writer, translator, "
					+ "publisher,language,date, reason,prices,number,operator) VALUES (" + "'" + escapeSql(ISBN) + "', "
					+ typeId + ", " + "'" + escapeSql(bookname) + "', " + "'" + escapeSql(writer) + "', " + "'"
					+ escapeSql(translator) + "', " + "'" + escapeSql(publisher) + "', " + "'" + escapeSql(languange)
					+ "', " + "'" + date + "', " + "'" + escapeSql(reason) + "'," + "'" + prices + "'," + "'" + nums
					+ "'," + "'" + escapeSql(operator) + "')";

			System.out.println("执行插入SQL: " + sql); // 调试用
			i = Dao.executeUpdate(sql);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		Dao.close();
		return i;
	}

	public static int InsertRecommender(String username, String ISBN) {
		int i = 0;
		try {
			// 修正 SQL 语句，添加引号
			String sql = "INSERT INTO tb_recommender(username,ISBN) VALUES (" + "'" + escapeSql(username) + "', " + "'"
					+ escapeSql(ISBN) + "')";

			System.out.println("执行插入SQL: " + sql); // 调试用
			i = Dao.executeUpdate(sql);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		Dao.close();
		return i;
	}

	public static int Updatebook(String ISBN, Integer typeId, String bookname, String writer, String translator,
			String publisher, String language, Date date, double price, Integer number) {
		int i = 0;
		try {
			// 修正 SQL 语句，添加引号和 WHERE 子句
			String sql = "UPDATE tb_bookInfo SET " + "typeId=" + typeId + ", " + "bookname='" + escapeSql(bookname)
					+ "', " + "writer='" + escapeSql(writer) + "', " + "translator='" + escapeSql(translator) + "', "
					+ "publisher='" + escapeSql(publisher) + "', " + "language='" + escapeSql(language) + "', "
					+ "date='" + date + "', " + "price=" + price + ", " + "number=" + number + " " + "WHERE ISBN='"
					+ escapeSql(ISBN) + "'";

			System.out.println("执行更新SQL: " + sql); // 调试用
			i = Dao.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close();
		return i;
	}

	public static int Updateorder(String ISBN, Integer typeId, String bookname, String writer, String translator,
			String publisher, String language, String reason) {
		int i = 0;
		try {
			// 修正 SQL 语句，添加引号和 WHERE 子句
			String sql = "UPDATE tb_bookInfo SET " + "typeId=" + typeId + ", " + "bookname='" + escapeSql(bookname)
					+ "', " + "writer='" + escapeSql(writer) + "', " + "translator='" + escapeSql(translator) + "', "
					+ "publisher='" + escapeSql(publisher) + "', " + "language='" + escapeSql(language) + "', "
					+ "reason='" + escapeSql(reason) + " WHERE ISBN='" + escapeSql(ISBN) + "'";

			System.out.println("执行更新SQL: " + sql); // 调试用
			i = Dao.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close();
		return i;
	}

//	// 重载一个可以修改ISBN的update
//	public static int Updatebook(String OldISBN,String ISBN, Integer typeId, String bookname,
//	        String writer, String translator, String publisher, Date date, double price,Integer number) {
//	    int i = 0;
//	    try {
//	        // 修正 SQL 语句，添加引号和 WHERE 子句
//	        String sql = "UPDATE tb_bookInfo SET " +
//	                "ISBN='" + escapeSql(ISBN) + "', " +
//	                "typeId=" + typeId + ", " +
//	                "bookname='" + escapeSql(bookname) + "', " +
//	                "writer='" + escapeSql(writer) + "', " +
//	                "translator='" + escapeSql(translator) + "', " +
//	                "publisher='" + escapeSql(publisher) + "', " +
//	                "date='" + date + "', " +
//	                "price=" + price + ", " +
//	                "number=" + number + " " +
//	                "WHERE ISBN='" + escapeSql(OldISBN) + "'";
//	        
//	        System.out.println("执行更新SQL: " + sql); // 调试用
//	        i = Dao.executeUpdate(sql);
//	    } catch (Exception e) {
//	        e.printStackTrace();
//	    }
//	    Dao.close();
//	    return i;
//	}
	public static int Deletebook(String ISBN) {
		int i = 0;
		try {
			// 修正 SQL 语句，添加引号和 WHERE 子句
			String sql = "Delete from tb_bookInfo  WHERE ISBN='" + escapeSql(ISBN) + "'";

			System.out.println("执行删除SQL: " + sql); // 调试用
			i = Dao.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close();
		return i;
	}

	public static int Deleteorder(String ISBN) {
		int i = 0;
		try {
			// 修正 SQL 语句，添加引号和 WHERE 子句
			String sql = "Delete from tb_order  WHERE ISBN='" + escapeSql(ISBN) + "'";

			System.out.println("执行删除SQL: " + sql); // 调试用
			i = Dao.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close();
		return i;
	}

	public static int DeleteRecommender(String ISBN) {
		int i = 0;
		try {
			// 修正 SQL 语句，添加引号和 WHERE 子句
			String sql = "Delete from tb_recommender  WHERE ISBN='" + escapeSql(ISBN) + "'";

			System.out.println("执行删除SQL: " + sql); // 调试用
			i = Dao.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close();
		return i;
	}

	public static Operator selectOperator(String sql)// 查阅Operator表
	{
		Operator operator = new Operator();// 操作员信息对象
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) {// 如果查询到了记录
				operator.setSex(rs.getString("sex"));// 设置操作员用户性别
				operator.setName(rs.getString("name"));// 设置操作员用户姓名
				operator.setIndentityCard(rs.getString("identityCard"));// 设置操作员用户身份证
				operator.setDate(rs.getDate("date"));// 设置操作员用户创建用户日期
				operator.setMaxNum(rs.getString("maxNum"));// 设置操作员用户最大借阅书的数量
				operator.setTel(rs.getString("tel"));// 设置操作员用户电话
				operator.setZj(rs.getInt("zj"));// 设置操作员用户类别
				operator.setZy(rs.getString("zy"));// 设置操作员用户职业
				operator.setISBN(rs.getString("R_ISBN"));// 设置操作员用户的借阅ISBN
				operator.setGrade(rs.getInt("admin"));// 设置操作员等级
				operator.setPassword(rs.getString("password"));// 设置管理员密码
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return operator;// 返回操作员信息对象
	}

	public static int InsertBorrow(String sql) {
		int i = 0;
		try {
			i = Dao.executeUpdate(sql); // 执行更新
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close();
		return i;
	}

	public static int executeUpdate(String sql, Object... params) {
		try {
			if (conn == null)
				new Dao();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject(i + 1, params[i]);
			}
			return pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}

	public static ResultSet executeQuery(String sql, Object... params) { // 可变参数适配任意数量参数
		ResultSet rs = null;
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstmt.setObject(i + 1, params[i]); // 设置参数
			}
			rs = pstmt.executeQuery(); // 执行查询
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	// 检查id是否重复
	public static boolean isId(int id) {
		String sql = "SELECT 1 FROM tb_booktype WHERE id = " + id;
		try (ResultSet rs = executeQuery(sql)) {
			return rs.next();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	// 检查类别名称是否重复
	public static boolean isTypeName(String typeName) {
		String sql = "SELECT 1 FROM tb_booktype WHERE typename = '" + typeName.replace("'", "''") + "'";
		try (ResultSet rs = executeQuery(sql)) {
			return rs.next();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	public static String selectReserveReaderISBN(String sql) {
		Reserve reserve = new Reserve();
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) { // 遍历结果集
				// 书籍信息对象

				reserve.setbookISBN(rs.getString("bookISBN"));
				reserve.setreaderISBN(rs.getString("readerISBN"));
				reserve.setreserveDate(rs.getDate("reserveDate"));
				// System.out.println(reserve.getreaderISBN());
				// list.add(reserve); //添加书籍信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return reserve.getreaderISBN();
	}

	public static List selectReserve(String sql) {
		System.out.println(sql);
		List list = new ArrayList();// 保存所有查询到书籍信息
		ResultSet rs = Dao.executeQuery(sql);// 执行查询
		try {
			while (rs.next()) { // 遍历结果集
				Reserve reserve = new Reserve(); // 书籍信息对象

				reserve.setbookISBN(rs.getString("bookISBN"));
				reserve.setreaderISBN(rs.getString("readerISBN"));
				reserve.setreserveDate(rs.getDate("reserveDate"));
				// System.out.println(reserve.getreaderISBN());
				list.add(reserve); // 添加书籍信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Dao.close(); // 关闭连接对象
		return list; // 返回查询结果集
	}
}
